package cn.yuanqiao.archive.detection.service.impl;

import cn.yuanqiao.archive.detection.domain.Detection;
import cn.yuanqiao.archive.detection.domain.InspectionLogs;
import cn.yuanqiao.archive.detection.mapper.InspectionLogsMapper;
import cn.yuanqiao.archive.detection.service.IInspectionLogsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 四性检测详情Service业务层处理
 * 
 * @author xy
 * @date 2023-08-10
 */
@Service
public class InspectionLogsServiceImpl implements IInspectionLogsService 
{
    @Autowired
    private InspectionLogsMapper inspectionLogsMapper;


    /**
     * 查询四性检测详情
     * 
     * @param id 四性检测详情主键
     * @return 四性检测详情
     */
    @Override
    public InspectionLogs selectInspectionLogsById(Long id)
    {
        return inspectionLogsMapper.selectInspectionLogsById(id);
    }

    /**
     * 查询四性检测详情列表
     * 
     * @param inspectionLogs 四性检测详情
     * @return 四性检测详情
     */
    @Override
    public List<InspectionLogs> selectInspectionLogsList(InspectionLogs inspectionLogs)
    {
        return inspectionLogsMapper.selectInspectionLogsList(inspectionLogs);
    }

    /**
     * 新增四性检测详情
     * 
     * @param inspectionLogs 四性检测详情
     * @return 结果
     */
    @Override
    public int insertInspectionLogs(InspectionLogs inspectionLogs)
    {
        return inspectionLogsMapper.insertInspectionLogs(inspectionLogs);
    }

    /**
     * 修改四性检测详情
     * 
     * @param inspectionLogs 四性检测详情
     * @return 结果
     */
    @Override
    public int updateInspectionLogs(InspectionLogs inspectionLogs)
    {
        return inspectionLogsMapper.updateInspectionLogs(inspectionLogs);
    }

    /**
     * 批量删除四性检测详情
     * 
     * @param ids 需要删除的四性检测详情主键
     * @return 结果
     */
    @Override
    public int deleteInspectionLogsByIds(Long[] ids)
    {
        return inspectionLogsMapper.deleteInspectionLogsByIds(ids);
    }

    /**
     * 删除四性检测详情信息
     * 
     * @param id 四性检测详情主键
     * @return 结果
     */
    @Override
    public int deleteInspectionLogsById(Long id)
    {
        return inspectionLogsMapper.deleteInspectionLogsById(id);
    }

    @Override
    public List<Detection> selectDetectionList(InspectionLogs inspectionLogs) {
        List<InspectionLogs> inspectionLogsList=inspectionLogsMapper.selectInspectionLogsList(inspectionLogs);
        List<Detection> detections=new ArrayList<>();
        if(inspectionLogsList.size()>0) {
            detections = inspectionLogsMapper.getDetection(inspectionLogsList);
        }
        return createTree(0L,detections,inspectionLogsList);
    }
    private List<Detection> createTree(Long pid, List<Detection> detections,List<InspectionLogs> inspectionLogsList) {
        List<Detection> treeList = new ArrayList<>();
        for (Detection detection : detections) {
            if (pid.equals(0)) {
                if (detection.getSuperId().equals(0)) {    //那这就是第一级节点

                    treeList.add(detection);
                    detection.setChildren(createTree(detection.getId(), detections, inspectionLogsList));
                }
            } else {
                if (pid.equals(detection.getSuperId())) {
                    if (detection.getdType().equals("3")) {
                        for (InspectionLogs inspectionLogs : inspectionLogsList) {
                            if (inspectionLogs.getdId().equals(detection.getId())) {
                                detection.setId(inspectionLogs.getId());
                                detection.setDetectionTime(inspectionLogs.getDetectionTime());
                                detection.setDetectionResult(inspectionLogs.getDetectionResult());
                                //获取该方案是否为默认通过
                                detection.setIsPass(inspectionLogs.getIsPass());
                                detection.setFailNum(inspectionLogs.getFailNum());
                            }
                         //   break;
                        }
                        treeList.add(detection);
                        detection.setChildren(createTree(detection.getdId(), detections, inspectionLogsList));
                    } else {
                        treeList.add(detection);
                        detection.setChildren(createTree(detection.getId(), detections, inspectionLogsList));

                    }
                }
            }
//            当children为[]的时候，将他设置成null
            if (detection.getChildren() == null || detection.getChildren().size() == 0) {
                detection.setChildren(null);
            }
        }
        return treeList;
    }

}
